package com.yxq.mdt.app.modules.bottomsheet;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.widgets.tagview.Tag;
import com.yxq.mdt.app.widgets.tagview.TagView;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @author L
 * @time 2019-10-30
 * @describe 选择喜欢类型
 */
public class PersonalLikeTypeSheetDialogFragment extends BottomSheetDialogFragment {
    Unbinder unbinder;
    @BindView(R.id.dialog_tv_number)
    TextView dialogTvNumber;
    @BindView(R.id.dialog_tv_save)
    TextView dialogTvSave;
    @BindView(R.id.recyclerView)
    TagView mTagView;
    String[] keys = new String[]{"御姐", "感性", "闷骚", "小清新", "人美声甜", "小仙女", "阳光", "萌萌哒", "逗逼", "理性", "专一", "文艺", "敢爱敢性", "宅", "天然呆", "女汉子", "小公主", "讲义气", "高冷", "内向"};
    HashMap<String, String> selectMap;

    public PersonalLikeTypeSheetDialogFragment(String loveType) {
        selectMap = new HashMap<>();
        //初始化选中数据
        if (loveType != null) {
            String[] data = loveType.split(",");
            for (String s : data) {
                for (int i = 0; i < keys.length; i++) {
                    if (s.equals(keys[i])) {
                        selectMap.put(i + "", s);
                        break;
                    }
                }
            }
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置背景透明，才能显示出layout中诸如圆角的布局，否则会有白色底（框）
        setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.personal_like_type_bottom_sheet_fragment_dialog, container, false);
        unbinder = ButterKnife.bind(this, view);
        initViews();
        return view;
    }

    private void initViews() {
        dialogTvNumber.setText(String.format("(%d/%d)", selectMap.size(), 6));
        initTags();
    }

    private void initTags() {
        mTagView.removeAllTags();
        for (int i = 0; i < keys.length; i++) {
            Tag tag = new Tag(keys[i]);
            if (selectMap.get(i + "") != null) {
                tag.tagTextColor = getResources().getColor(R.color.white);
                tag.layoutColor = getResources().getColor(R.color.color_font_red);
                tag.layoutColorPress = getResources().getColor(R.color.color_font_red);
            } else {
                tag.tagTextColor = getResources().getColor(R.color.color_font_black_666);
                tag.layoutColor = getResources().getColor(R.color.bg_gray);
                tag.layoutColorPress = getResources().getColor(R.color.gray);
            }
            tag.radius = 60f;
            tag.tagTextSize = 14f;
            tag.layoutBorderSize = 1f;
            tag.layoutBorderColor = getResources().getColor(R.color.bg_gray);
            tag.isDeletable = false;
            mTagView.addTag(tag);
        }
        mTagView.setOnTagClickListener((position, tag) -> {
            if (selectMap.containsKey(position + "")) {
                selectMap.remove(position + "");
            } else {
                if (selectMap.size() >= 6) {
                    ToastUtils.showShort("每一项最多只能选择6个哦");
                    return;
                }
                selectMap.put(position + "", keys[position]);
            }
            dialogTvNumber.setText(String.format("(%d/%d)", selectMap.size(), 6));
            initTags();
        });
    }

    PersonalLikeTypeSheetDialogFragmentInterface personalLikeTypeSheetDialogFragmentInterface;

    public void setPersonalLikeTypeSheetDialogFragmentInterface(PersonalLikeTypeSheetDialogFragmentInterface personalLikeTypeSheetDialogFragmentInterface) {
        this.personalLikeTypeSheetDialogFragmentInterface = personalLikeTypeSheetDialogFragmentInterface;
    }

    public interface PersonalLikeTypeSheetDialogFragmentInterface {
        void checked(HashMap<String, String> data);
    }

    @OnClick(R.id.dialog_tv_save)
    public void onViewClicked() {
        dismiss();
        if (personalLikeTypeSheetDialogFragmentInterface != null) {
            personalLikeTypeSheetDialogFragmentInterface.checked(selectMap);
        }
    }
}
